frame = 10;
FOI = strfind(frame_id, [0, frame]);
FOI2 = strfind(frame_id, [frame, 0]);

indices_b = [];
indices_p = [];
for ind=1:length(FOI)
    indices_b = [indices_b FOI(ind)-15:FOI(ind)];
    indices_p = [indices_p FOI(ind)+1:FOI2(ind)];

    bkg2(:,:,ind) = fliplr(mean(vdata(:,:,FOI(ind)-15:FOI(ind)),3));
    patch2(:,:,ind) = fliplr(mean(vdata(:,:,FOI(ind)+1:FOI2(ind)),3)) - bkg2(:,:,ind);
        
    bkg3(:,:,ind) = fliplr(mean(vdata(:,:,indices_b),3));
    patch3(:,:,ind) = fliplr(mean(vdata(:,:,indices_p),3)) - bkg3(:,:,ind);
end

pixel_in_x = size(vdata,2);
fov_in_um = 200;
lowpass = 2;
lowpass_pix = lowpass * pixel_in_x / fov_in_um;
kernel_size = ceil(lowpass_pix * 5);
sp_filter = fspecial('gaussian', kernel_size, lowpass_pix); 
patch_f = zeros(size(vdata,1),size(vdata,2),prod(patches));
for ind = 1:size(patch2,3)
    patch_f(:,:,ind) = Filter2Modified(sp_filter, patch2(:,:,ind)-mean2(patch2(:,:,ind)));
end
patch_f2 = zeros(size(vdata,1),size(vdata,2),prod(patches));
for ind = 1:size(patch2,3)
    patch_f2(:,:,ind) = Filter2Modified(sp_filter, patch3(:,:,ind)-mean2(patch3(:,:,ind)));
end

% single trials
figure
montage(permute(patch_f,[1, 2, 4, 3]), 'Size', [5 4])
set(gca,'clim',[min(patch_f(:))*0.6 max(patch_f(:))])

% accumulate trials
figure
montage(permute(patch_f2,[1, 2, 4, 3]), 'Size', [5 4])
set(gca,'clim',[min(patch_f2(:))*0.6 max(patch_f2(:))])


 